#include <eventloop/IOchannel.h>
#include <eventloop/Eventloop.h>
#include <utility/chrono.h>
#include <functional>
#include <unistd.h>
#include <stdio.h>
#include <sys/timerfd.h>
#include <cstring>

Eventloop* g_loop;
int timerfd;

void timeout(Utility::timepoint receiveTime)
{
	printf("Timeout......!\n");
	uint64_t howmany;
	::read(timerfd, &howmany, sizeof howmany);
	g_loop->quit();
}

int main(void)
{
	Eventloop loop;
	g_loop = &loop;

	timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
	IOchannel channel{loop, timerfd};
	channel.set_read_callback(timeout);
	channel.enable_reading();	

	struct itimerspec howlong;
	bzero(&howlong, sizeof howlong);
	howlong.it_value.tv_sec = 1;
	::timerfd_settime(timerfd, 0, &howlong, NULL);

	loop.loop();

	::close(timerfd);
}



